require "xmath.lua"
KaCaEffect = {
    kacaNode = nil,
    kacaProgress = 0.0,
    scene = nil,
    kacatype=1, --默认为1
}
KaCaEffectType={
  effectType_fromLeft=1,
  effectType_fromRight=2,
  effectType_light=3,
}
function KaCaEffect:new(scene,type)
    local newObj = {
    }
    setmetatable(newObj, self)
    self.__index = self
    self.scene = scene
    self.kacatype = type
    self.kacaProgress = 0.0
    self.kacaNode = self:getKacaEffectNode()
    return newObj
end
function KaCaEffect:getKacaEffectNode()
    local shaderName = nil
    if(self.kacatype==KaCaEffectType.effectType_fromLeft)then
            shaderName="kaca1.frag"
    elseif(self.kacatype==KaCaEffectType.effectType_fromRight)then
            shaderName="kaca2.frag"
    elseif(self.kacatype==KaCaEffectType.effectType_light)then
            shaderName="kaca3.frag"
    else
        --....其它
    end
    local g_kacaNode = KaCaEffect:addNodeAndRelease(self.scene,KuruShaderFilterNode.createWithFragmentShaderFile(BASE_DIRECTORY..shaderName,true))
    g_kacaNode:setEnabled(false) --咔擦动画，每点击一次就屏幕就闪一下 
    return g_kacaNode 
end
function KaCaEffect:addNodeAndRelease(scene, node)
    scene:addNode(node)
    node:release()
    return node
end
function KaCaEffect:begainKacaAni(sample)
        self.kacaNode:setEnabled(true)
        self.kacaNode:setChannel0(sample)
end
function KaCaEffect:endKacaAni()
    self.kacaNode:setEnabled(false)
    self.kacaProgress = 0.0
end
function KaCaEffect:updateKacaProgress(progress)
    if progress<0 then
        progress=0
    elseif progress>1 then
        progress=1
    end
    if(self.kacatype==KaCaEffectType.effectType_fromLeft)then
        self.kacaNode:getMaterial():getParameter("whiteIntensity"):setFloat(lerpFloat(0,1,progress))
    elseif(self.kacatype==KaCaEffectType.effectType_fromRight)then
        self.kacaNode:getMaterial():getParameter("whiteIntensity"):setFloat(lerpFloat(0,1,progress))
    elseif(self.kacatype==KaCaEffectType.effectType_light)then
        self.kacaNode:getMaterial():getParameter("whiteIntensity"):setFloat(pingpongLerp(0,1,progress))
    else
    --....其它
    end
   
end